package scau.erps.view.base;

import java.awt.GridLayout;
import java.util.List;

import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.border.TitledBorder;

import scau.erps.app.Erps;
import scau.erps.entity.Factory;
import scau.erps.entity.Line;
import scau.erps.service.FactoryService;
import scau.erps.service.LineService;
import scau.erps.service.impl.FactoryServiceImpl;
import scau.erps.service.impl.LineServiceImpl;

/**
 * 生产线操作父级窗口
 * 
 * @author outsider
 *
 */
public class LineDialog extends JDialog {
	private static final long serialVersionUID = 1L;
	protected JPanel[] lineJPanel = new JPanel[8]; // 生产线面板
	protected JComboBox<String>[] typeComboBox = new JComboBox[8]; // 生产线种类下拉框
	protected JComboBox<String>[] productComboBox = new JComboBox[8];// 产品类型下拉框
	protected int team_Id = Erps.getCurrentTeam().getId();
	protected LineService lineService = Erps.getIocInstance().get(LineServiceImpl.class);
	protected FactoryService factoryService = Erps.getIocInstance().get(FactoryServiceImpl.class);
	protected JButton okButton, updateButton, usuriousLoanButton, rentFactoryButton;
	protected final static String[] TYPE = new String[] { "空置", "手工线", "半自动", "全自动", "柔性线" };
	protected final static String[] PRODUCT = new String[] { "无", "Beryl", "Crystal", "Ruby", "Sapphire" };

	public LineDialog() {
		setTitle("投资新生产线");
		this.setModal(true);
		this.setSize(600, 450);
		this.setLocationRelativeTo(null);
		this.setDefaultCloseOperation(0);
		JPanel rootPanel = new JPanel();
		rootPanel.setLayout(null);
		this.setContentPane(rootPanel);
		JPanel bigFactoryPanel = new JPanel();
		bigFactoryPanel.setBounds(0, 0, 540, 160);
		bigFactoryPanel.setBorder(new TitledBorder(null, "\u5927\u5382\u623F", TitledBorder.LEADING, TitledBorder.TOP, null, null));
		rootPanel.add(bigFactoryPanel);
		bigFactoryPanel.setLayout(new GridLayout(1, 4, 0, 0));
		JPanel middleFactoryPanel = new JPanel();
		middleFactoryPanel.setBounds(0, 160, 540, 160);
		middleFactoryPanel.setBorder(new TitledBorder(null, "\u4E2D/\u5C0F\u5382\u623F", TitledBorder.LEADING, TitledBorder.TOP, null, null));
		rootPanel.add(middleFactoryPanel);
		middleFactoryPanel.setLayout(new GridLayout(1, 4, 0, 0));
		okButton = new JButton("确定");
		okButton.setBounds(350, 330, 80, 25);
		getContentPane().add(okButton);
		updateButton = new JButton("支付费用");
		updateButton.setBounds(450, 330, 80, 25);
		getContentPane().add(updateButton);
		updateButton.setEnabled(false);
		usuriousLoanButton = new JButton("高利贷");
		usuriousLoanButton.setBounds(250, 330, 80, 25);
		getContentPane().add(usuriousLoanButton);
		usuriousLoanButton.setEnabled(true);
		rentFactoryButton = new JButton("租用厂房");
		rentFactoryButton.setBounds(150, 330, 80, 25);
		getContentPane().add(rentFactoryButton);
		rentFactoryButton.setEnabled(true);
		JPanel panel = new JPanel();
		panel.setBounds(0, 340, 540, 113);
		getContentPane().add(panel);
		for (int i = 0; i <= 7; i++) {
			lineJPanel[i] = new JPanel();
			bigFactoryPanel.add(lineJPanel[i]);
			lineJPanel[i].setLayout(null);
			JLabel label = new JLabel("生产线");
			label.setBounds(35, 20, 60, 15);
			lineJPanel[i].add(label);
			typeComboBox[i] = new JComboBox<String>();
			typeComboBox[i].setModel(new DefaultComboBoxModel(TYPE));
			typeComboBox[i].setBounds(20, 45, 90, 25);
			typeComboBox[i].setEditable(false);
			lineJPanel[i].add(typeComboBox[i]);
			JLabel label_1 = new JLabel("产品类型");
			label_1.setBounds(30, 80, 60, 15);
			lineJPanel[i].add(label_1);
			productComboBox[i] = new JComboBox<String>();
			productComboBox[i].setModel(new DefaultComboBoxModel(PRODUCT));
			productComboBox[i].setBounds(20, 100, 90, 25);
			productComboBox[i].setEnabled(false); // 在按下确认按钮前锁定更新按钮 防止BUG
			lineJPanel[i].add(productComboBox[i]);
			if (i <= 3)
				bigFactoryPanel.add(lineJPanel[i]);
			else
				middleFactoryPanel.add(lineJPanel[i]);
		}
		getLineStatus();
	}

	protected void getLineStatus() {
		List<Line> listLine = lineService.getLineByTeamId(team_Id);
		// 获取该小组生产线状态
		for (int i = 0; i <= 7; i++) {
			String status = listLine.get(i).getStatus();
			String type = listLine.get(i).getType();
			String product = listLine.get(i).getProduct();
			lineJPanel[i].setBorder(new TitledBorder(null, "Line" + (i + 1) + " " + status, TitledBorder.LEADING, TitledBorder.TOP, null, null)); // 设置每个小panel的状态
			if (!status.equals("空置")) {
				typeComboBox[i].setSelectedItem(type);
				productComboBox[i].setSelectedItem(product);
			}
		}
	}
}
